package pl.un4giv3n.mightvsmagic.action;

import java.util.List;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Action;
import pl.un4giv3n.mightvsmagic.entity.ActionType;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.SpecialSkill;
import pl.un4giv3n.mightvsmagic.entity.SpecialSkillTemplate;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.utils.ActionUtil;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;
import pl.un4giv3n.mightvsmagic.utils.UserUtil;

/**
 * @author andrzejewskia action performing buing of an item on the bazar
 */
public class LearnSkillAction extends BazarPageAction {

	private static final long serialVersionUID = 1L;
	/**
	 * session user
	 */
	private User user;
	/**
	 * bought item id
	 */
	private long skillId;

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		Action currentAction = user.getCurrentAction();
		if (currentAction == null || currentAction.equals(Action.NO_ACTION)
				|| currentAction.getActionType().equals(ActionType.FIGHT)) {
			SpecialSkillTemplate template = entityManager.find(
					SpecialSkillTemplate.class, skillId);
			entityManager.getTransaction().begin();
			List<SpecialSkillTemplate> avaibleSpecialSkills = UserUtil.getAvaibleToLearnSpecialSkills(user, entityManager);
			if (!canAfford(template)) {
				addActionError("Nie stac cię na tą umiejętność");
				entityManager.getTransaction().rollback();
				return INPUT;
			} else if(!avaibleSpecialSkills.contains(template)){
				addActionError("Nie spełniasz wymagań by nauczyć się tej umiejętności");
				entityManager.getTransaction().rollback();
				return INPUT;
			}
			else{
				SpecialSkill skill = new SpecialSkill(template.getName(),
						template.getDmgFactor(), template.getDefFactor(), template.getSpeedFactor(),
						template.getMinDmgIncrease(), 
						template.getMaxDmgIncrease(), 
						template.getMinDefIncrease(), 
						template.getMaxDefIncrease(), 
						template.getStrenghtModifier(), 
						template.getStaminaModifier(), 
						template.getManaModifier(), 
						template.getAttackModifier(), 
						template.getParryModifier(), template.getSpeedModifier(), template.getPrice(),
						template.getIceDamage(), template.getFireDamage(),
						template.getEarthDamage(), template.getWindDamage(),
						template.getIceDefence(), template.getFireDefence(),
						template.getEarthDefence(), template.getWindDefence(),
						template.getId(), template.getRequiresToLearn(),template.getBlocksLearning());
				entityManager.persist(skill);
				user.getSkills().add(skill);
				user.removeGold(skill.getPrice());
				user.setDragonTeeth(user.getDragonTeeth() - template.getPremiumPrice());
				UserActivity activity = ActivityLogUtil.createActivity(ActivityType.LEARN_SKILL, user.getId(), "Nauczony skill: " + skill.toString());
				entityManager.persist(activity);
				entityManager.merge(user);
				entityManager.flush();
				entityManager.getTransaction().commit();
				entityManager.close();
				addActionMessage("Dokonałeś zakupu przedmiotu!");
				return SUCCESS;
			}
		} else {
			entityManager.close();
			addActionError(ActionUtil.generateErrorMessage(currentAction, user));
			return ERROR;
		}
	}

	private boolean canAfford(SpecialSkillTemplate template) {
		return template.getPrice() <= user.getGold() && template.getPremiumPrice() <= user.getDragonTeeth();
	}

	public long getSkillId() {
		return skillId;
	}

	public void setSkillId(long skillId) {
		this.skillId = skillId;
	}
}
